<template>
    <div class="nav">

        <span @click="signUp = true">注册</span>
        <span @click="signIn = true">登录</span>
        <span v-text="username.name"> </span>
    </div>
    <el-dialog v-model="signUp" title="用户注册" width="30%" center>
        <el-from :model="signUpForm" label-width="80px">
            <el-form-item label="">
                <el-input v-model="signUpForm.username" placeholder="username"/>
            </el-form-item>
            <el-form-item label="">
                <el-input type="password" v-model="signUpForm.password" placeholder="password" show-password/>
            </el-form-item>
            <el-form-item label="">
                <el-input type="text" v-model="signUpForm.age" placeholder="age"/>
            </el-form-item>
            <el-form-item label="">
                <el-input type="text" v-model="signUpForm.phone" placeholder="phone"/>
            </el-form-item>
            <el-form-item label="">
                <el-input type="date" v-model="signUpForm.birthday" placeholder="birthday"/>
            </el-form-item>
            <el-form-item label="">
                <el-input type="email" v-model="signUpForm.email" placeholder="email"/>
            </el-form-item>
            <el-from-item>
                <el-button type="primary" @click="onSignUpForm()">sign up</el-button>
            </el-from-item>
        </el-from>
    </el-dialog>
    <el-dialog v-model="signIn" title="登录" width="30%" center>
        <el-from :model="signInForm" label-width="80px">
            <el-form-item label="">
                <el-input type="text" v-model="signInForm.username" placeholder="username"/>
            </el-form-item>
            <el-form-item label="">
                <el-input type="password" v-model="signInForm.password" placeholder="password" show-password/>
            </el-form-item>
            <div class="mb-2 flex items-center text-sm" style="margin-bottom: 10px">
                <el-radio-group v-model="radio1" class="ml-4">
                    <el-radio label="1" size="large" @change="getRadio()">用户登录</el-radio>
                    <el-radio label="0" size="large" @change="getRadio()">管理员登录</el-radio>
                </el-radio-group>
            </div>
            <el-from-item style="margin-top: 10px">
                <el-button type="primary" @click="onSignInForm()">sign In</el-button>
            </el-from-item>
        </el-from>
    </el-dialog>
    <div class="nav-search">
        <el-row>
            <el-col :span="6">
                <h2>FastShopping</h2>
            </el-col>
            <el-col :span="7">
            </el-col>
            <el-col :span="11">
                <el-input v-model="input2" placeholder="Please input" style="border:2px solid black;">
                    <template #prepend>
                        <el-button :icon="Search"/>
                    </template>
                </el-input>
            </el-col>
        </el-row>
    </div>
    <div class="nav-title">
        <ul>
            <li><a>首页</a>
            </li>
            <li><a>服饰鞋包</a></li>
            <li><a @click="digital()">数码家电</a></li>
            <li><a>美食酒水</a></li>
        </ul>
    </div>
    <el-carousel indicator-position="outside" height="310px" style="clear: left;width: 1080px;margin: auto">
        <el-carousel-item v-for="item in 4" :key="item">
        </el-carousel-item>
    </el-carousel>
    <div class="common-layout">
        <el-container>
            <el-main>
                <h2 style="margin-bottom: 20px">人气推荐</h2>
                <el-row gutter="20">
                    <el-col :span="6" v-for="product in products" :key="product">
                        <el-card class="product-card">
                            <div class="info">
                                <el-image class="product-picture" :src="`http://127.0.0.1:8081/product1/${product.picture}`"></el-image>

                                <div>
                                    <el-text type="info" truncated>{{ product.description }}</el-text>
                                </div>
                                <div>
                                    <el-space>
                                        <el-text type="danger">{{product.price}}￥</el-text>
                                    </el-space>
                                </div>
                            </div>
                        </el-card>
                    </el-col>
                </el-row>


            </el-main>
        </el-container>
    </div>
    <div class="footer">
        <h3>FastShopping@copy2023</h3>
    </div>
</template>
<script lang="ts" setup>
import {ref} from 'vue'
import {reactive} from "vue";
import {Search, User} from '@element-plus/icons-vue'
import router from '../router/index.js'
import axios from 'axios'

const input2 = ref('')

const signUp = ref(false)
const signIn = ref(false)

const signUpForm = reactive({
    username: '',
    password: '',
    age: '',
    phone: '',
    birthday: '',
    email: ''
})
const onSignUpForm = () => {
    console.log(signUpForm);
    axios.post('http://localhost:8080/user/create', signUpForm).then(res => {
        console.log(res.data);
        if (res.data) {
            alert("注册成功,请登录");
            signUp.value = false;

        } else {
            alert("注册失败")
        }
    }).catch(error => {
        console.log(error)
    })
}
const signInForm = reactive({
    username: '',
    password: ''
})
const username = reactive({
        name: ''
    })
const radio1 = ref(
    '1'
);
const getRadio = () => {
    radio1.value
    console.log(radio1.value)
}
const onSignInForm = () => {
    if (signInForm.username.length === 0) {
        confirm("请输入用户名");
    } else {
        if (signInForm.password.length == 0) {
            alert("请输入密码");
        } else {
            if (radio1.value == "1") {
                axios.post('http://localhost:8080/user/login', signInForm).then(res => {
                    console.log(res.data);
                    if (res.data) {
                        alert("登录成功");
                        signIn.value = false;
                        username.name = res.data.username;

                        console.log(this.username.name)
                    } else {
                        alert("登录失败")
                    }
                }).catch(error => {
                    console.log(error)
                })
            }
            if (radio1.value == "0") {
                axios.post('http://localhost:8080/admin/login', signInForm).then(res => {
                    console.log(res.data);
                    if (res.data) {
                        alert("登录成功");
                        console.log(res.data);
                        let admins = res.data.username;
                        sessionStorage.setItem('adm',admins);
                        signIn.value = false;
                        router.push('/admin')

                    } else {
                        alert("登录失败")
                    }
                }).catch(error => {
                    console.log(error)
                })
            }
        }
    }

}

const digital = () => { // function () {
    router.push('/digital')
}
//商品显示

const products = ref([]);

axios.get('http://127.0.0.1:8080/product/show')
    .then(res => {
        products.value = res.data
    }).catch(err =>{
    console.error(err)
})
</script>
<style scoped>
.nav {
    position: relative;
    height: 40px;
    line-height: 36px;
    background-color: #333;
    z-index: 5;
    color: #ffffff;
}

.span1 {
    font-family: "Arial Black";
}

.nav span {
    float: right;
    padding-right: 20px;
    cursor: pointer;

}

.nav-search {
    width: 1080px;
    margin: auto;
    background: #fefefe;
    padding: 10px 0px 10px 0px;
}

.el-carousel__item:nth-child(2n) {
    background-image: url("http://127.0.0.1:8081/lunbo/1.webp");


}

.el-carousel__item:nth-child(2n + 1) {
    background-image: url("http://127.0.0.1:8081/lunbo/2.webp");
}

.common-layout {
    width: 1080px;
    margin: auto;
    background: #fefefe;
}

.product-card {
    margin-bottom: 20px;
}

.info {
    text-align: center;
}

.product-card:hover {
    cursor: pointer;
    background: gainsboro;

}
.product-card .product-picture{
    width:200px;
    height:200px;
    transition:transform 0.5s;
}
.product-card .product-picture :hover{
    transform: scale(1.5);
}
.demo-tabs > .el-tabs__content {
    padding: 32px;
    color: #6b778c;
    font-size: 32px;
    font-weight: bolder;
}

.nav-title {
    width: 1080px;
    margin: auto;
    padding-top: 10px;
    padding-bottom: 40px;

}

.nav-title ul {
    list-style: none;

}

.nav-title ul li {
    float: left;
    padding-right: 40px;
    font-size: 18px;
    font-weight: bolder;
    font-family: "Arial Black";

}

.nav-title ul li a:hover {
    color: orange;
    cursor: pointer;
}

.sign_up_modal {
    display: none;
    position: fixed;
    z-index: 999;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    background: rgba(0, 0, 0, .8);
}

.footer {
    height: 150px;
    background: black;
    position: relative;
    color: white;
}

.footer h3 {
    padding-top: 40px;
    padding-left: 45%;
}
</style>
